<template>
  <div class="ai-exam">
    <div class="chapter-list" v-if="examList.length > 0">
      <div
        class="chapter-list-item"
        :title="item.label"
        v-for="item in examList"
        :key="item.id"
        :class="{ active: selectItemId == item.id }"
        @click="handleClick(item)"
      >
        <span class="agent-name" :title="item.agentName">{{
          item.agentName
        }}</span>
        <span class="exam-status">{{
          item.status == 1 ? "已考核完毕" : "未考完试"
        }}</span>
      </div>
    </div>
    <div
      class="chapter-chat"
      :style="{ width: examList.length > 0 ? '80%' : '100%' }"
    >
      <CommonChat_stuAiExam
        v-if="selectItemId && examList.length > 0"
        :id="chapterId"
        :cId="classId"
        ref="commonChatRef"
        @endFn="endFn"
      />
      <div class="no-data" v-else>
        <img src="@/assets/img/no-data.png" alt="" />
        <div class="no-data-text">暂无内容</div>
      </div>
    </div>
  </div>
</template>

<script>
import CommonChat_stuAiExam from "../../components/commonChat_stuAiExam.vue";
export default {
  components: {
    CommonChat_stuAiExam,
  },
  data() {
    return {
      selectItemId: 1,
      examList: [],
      courseId: "",
      chapterId: null,
      classId: null,
      endStatus: null,
    };
  },
  created() {
    this.courseId = JSON.parse(localStorage.getItem("courseId"));
    this.getExamList();
  },
  methods: {
    // 获取章节
    getExamList() {
      this.$axios
        .get(`/tes/course/auto/make/running-assess?courseId=${this.courseId}`)
        .then((res) => {
          this.examList = res.data;
          if (this.examList && this.examList.length > 0) {
            this.chapterId = Number(this.examList[0].id);
            this.selectItemId = this.examList[0].id;
            this.classId = Number(this.examList[0].classId);
          }
        });
    },
    handleClick(item) {
      // 如果点击的是当前已选中的项目，不执行任何操作
      if (this.selectItemId === item.id) {
        return;
      }

      this.selectItemId = item.id;
      this.chapterId = Number(item.id);
      this.classId = Number(item.classId);

      // 切换章节时重置右侧对话
      this.$nextTick(() => {
        if (this.$refs.commonChatRef) {
          if (this.endStatus == 1) return;
          this.$refs.commonChatRef.resetChat();
        }
      });
    },

    // 子组件的考核状态
    endFn(endStatus) {
      this.endStatus = endStatus;
    },
  },
};
</script>

<style lang="scss" scoped>
.ai-exam {
  overflow-x: hidden;
  display: flex;
  width: 100%;
  overflow: hidden;
  box-sizing: border-box;
  min-height: 650px;
  .chapter-list {
    width: 20%;
    background-color: #fbfcff;
    padding: 30px 20px 44px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    max-height: 650px;
    box-sizing: border-box;
    &::-webkit-scrollbar {
      width: 4px;
    }

    &::-webkit-scrollbar-track {
      background: transparent;
      border-radius: calc(4px / 2);
    }

    &::-webkit-scrollbar-thumb {
      background: #c1c1c1;
      border-radius: calc(4px / 2);

      &:hover {
        background: #a8a8a8;
      }
    }
    .chapter-list-item {
      width: 100%;
      font-size: 16px;
      color: #6b6a85;
      flex-shrink: 0;
      padding: 15px 30px;
      box-sizing: border-box;
      cursor: pointer;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .agent-name {
        max-width: 150px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .exam-status {
        flex-shrink: 0;
        font-size: 12px;
        color: #999;
      }
      &.active {
        background: #e8efff;
        border-radius: 20px;
        color: #457aff;
      }
    }
  }
  .chapter-chat {
    width: 80%;
    .no-data {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding: 263px 0;
      gap: 10px;
      width: 100%;
      background-color: #f3f5fa;
      .no-data-text {
        font-size: 16px;
        color: #999;
      }
    }
  }
}
</style>
